package com.jml.jm_usb

import android.hardware.usb.UsbConfiguration
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbEndpoint
import android.hardware.usb.UsbInterface

object Util  {
    //USB设备转换为Map
    fun usbDeviceToMap(identifier :String,device: UsbDevice): Map<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["identifier"] = identifier
        map["deviceName"] = device.deviceName
        map["manufacturerName"] = device.manufacturerName?:""
        map["productName"] = device.productName?:""
        map["serialNumber"] = ""
        map["deviceClass"] = device.deviceClass
        map["deviceSubclass"] = device.deviceSubclass
        map["deviceProtocol"] = device.deviceProtocol
        map["vendorId"] = device.vendorId
        map["productId"] = device.productId
        map["configurationCount"] = device.configurationCount

        val configurations = mutableListOf<Map<String, Any>>()
        for (i in 0 until device.configurationCount) {
            configurations.add(usbConfigurationToMap(device.getConfiguration(i)))
        }
        map["configurations"] = configurations
        return map
    }

    //USB端点转换为Map
    private fun usbEndpointToMap(endpoint: UsbEndpoint): Map<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["endpointNumber"] = endpoint.endpointNumber
        map["direction"] = endpoint.direction
        map["type"] = endpoint.type
        map["attributes"] = endpoint.attributes
        map["maxPacketSize"] = endpoint.maxPacketSize
        map["interval"] = endpoint.interval
        return map
    }

    //USB接口转换为Map
    private fun usbInterfaceToMap(usbInterface: UsbInterface): Map<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["id"] = usbInterface.id
        map["alternateSetting"] = usbInterface.alternateSetting
        map["interfaceClass"] = usbInterface.interfaceClass
        map["interfaceSubClass"] = usbInterface.interfaceSubclass
        map["interfaceProtocol"] = usbInterface.interfaceProtocol
        val endpoints = mutableListOf<Map<String, Any>>()
        for (i in 0 until usbInterface.endpointCount) {
            endpoints.add(usbEndpointToMap(usbInterface.getEndpoint(i)))
        }
        map["endpoints"] = endpoints
        return map
    }

    //USB配置转换为Map
    private fun usbConfigurationToMap(configuration: UsbConfiguration): Map<String, Any> {
        val map = mutableMapOf<String, Any>()

        map["id"] = configuration.id
        map["maxPower"] = configuration.maxPower
        map["name"] = configuration.name ?: ""
        val interfaces = mutableListOf<Map<String, Any>>()
        for (i in 0 until configuration.interfaceCount) {
            interfaces.add(usbInterfaceToMap(configuration.getInterface(i)))
        }
        map["interfaces"] = interfaces
        return map
    }

    //USB Accessory转换为Map
    fun usbAccessoryToMap(accessory: android.hardware.usb.UsbAccessory): Map<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["uri"] = accessory.uri ?: ""
        map["description"] = accessory.description ?: ""
        map["manufacturer"] = accessory.manufacturer ?: ""
        map["model"] = accessory.model ?: ""
        map["serial"] = accessory.serial ?: ""
        map["version"] = accessory.version ?: ""
        return map
    }
}